extends Control

var deploy_scene : PackedScene = preload("res://UIScenes/DeploymentUI.tscn")

var deploy_instance
onready var unit_info_instance = $UnitInfo
var mouse_on_panel := false

onready var cursor := $"%Cursor"

func _ready():
	visible = true
	$VSplitContainer/Panel.visible = false
	unit_info_instance.visible = false
	Globals.current_mode = Globals.GAME_MODES.DEPLOYMENT
	deploy_instance = deploy_scene.instance()
	add_child(deploy_instance)
	deploy_instance.connect("mouse_on_panel", self, "deployment_mouse_on_panel")
	deploy_instance.connect("select_unit", self, "deployment_select_unit")
	deploy_instance.connect("play", self, "_on_deploy_play")
	
func _process(delta):
	if not mouse_on_panel:
		cursor.set_process_input(true)
		cursor.visible = true
	else:
		cursor.set_process_input(false)
		cursor.visible = false

func deployment_mouse_on_panel(v):
	mouse_on_panel = v

func deployment_select_unit(unit):
	cursor.selected_unit = unit

func _on_deploy_play():
	Globals.current_mode = Globals.GAME_MODES.PLAY
	deploy_instance.queue_free()
	cursor.set_process_input(true)
	cursor.visible = true
	cursor.texture = cursor.default_tex
	get_node("/root/World/").deploy_zone_instance.visible = false
	$VSplitContainer/Panel.visible = false

func _on_Selection_unit_selected(u: Unit):
	$VSplitContainer/Panel/HBoxContainer/TextureRect.texture = u.get_sprite()
	$VSplitContainer/Panel/HBoxContainer/WeaponInfo.set_featured_weapon(u.weapon_instance)
	unit_info_instance.set_featured_unit(u)
	unit_info_instance.visible = true

func _on_Selection_blank_selection():
	$VSplitContainer/Panel/HBoxContainer/TextureRect.texture = null
	unit_info_instance.visible = false
